package thinkingInJava.containers.Test_;

import java.util.*;
import thinkingInJava.containers.*;

public class SimpleHashMap3<K,V> extends SimpleHashMap<K,V> {
	@SuppressWarnings("unchecked")
	public void clear() {
		// Effectively erase everything by allocating
		// a new empty array of buckets:
		buckets = new LinkedList[SIZE];
	}
	public V remove(Object key) {
		// Code is copied from get(), except that
		// before returning the value, the Map.Entry is
		// removed from the list:
		int index = Math.abs(key.hashCode()) % SIZE;
		if(buckets[index] == null) return null;
		Iterator<MapEntry<K,V>> it = buckets[index].iterator();
		while(it.hasNext()) {
			MapEntry<K,V> iPair = it.next();
			if(iPair.getKey().equals(key)) {
				// Changes are here:
				V value = iPair.getValue();
				// Removes the last fetched value:
				it.remove();
				return value;
			}
		}
		return null;
	}
}
